<?php

/**
 * Controller_sanitation
 * 卫生
 */

class Controller_sanitation extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->semester		= YOU::loadModel('semester');
		$this->model->class			= YOU::loadModel('class');
		$this->model->sanitation	= YOU::loadModel('sanitation');

	}

	/** 首页 **/
	public function actionIndex()
	{

	}

	/** ---------------------------  分数值设置  --------------------------- **/
	/** 卫生分数值列表 **/
	public function actionScoreList()
	{
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_sanitation_score`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$score_lists = $this->db->fetchAll("SELECT * FROM `tbl_sanitation_score` ORDER BY score_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("score_lists", $score_lists);
		$this->tpl->display('sanitation/score.list.tpl.php');
	}
	/** 卫生分数值添加 **/
	public function actionScoreAdd()
	{

		if ($_POST){

			$score_value   	= $this->post->score_value;
			$score_lists	= $this->db->fetchAll("SELECT * FROM `tbl_sanitation_score` WHERE score_value = {$score_value}");

			if (!empty($score_lists)){
				Helper_Client::alert("分数值已经存在，请从新填写");
			}

			// 写入数据库
			$info = array(

			"score_value"			=> $score_value,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_insert("tbl_sanitation_score", $info);
			Helper_Client::location("添加成功", "index.php?a=sanitation.scorelist");

		}
		$this->tpl->display("sanitation/score.add.tpl.php");
	}
	/** 卫生分数值修改 **/
	public function actionScoreUpdate()
	{
		$score_id     		= $this->get->score_id;
		$score_data			= $this->db->fetchRow("SELECT * FROM `tbl_sanitation_score` WHERE score_id = {$score_id}");
		if (empty($score_data)){
			Helper_Client::alert("分数值已经存在，请从新填写");
		}
		if ($_POST){

			$score_value   	= $this->post->score_value;
			$score_da		= $this->db->fetchRow("SELECT * FROM `tbl_sanitation_score` WHERE score_value = {$score_value}");

			if (!empty($score_da)){
				Helper_Client::alert("分数值已经存在，请从新填写");
			}

			// 写入数据库
			$info = array(

			"score_value"			=> $score_value,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_update("tbl_sanitation_score", $info, "score_id='{$score_id}'");
			Helper_Client::location("修改成功", "index.php?a=sanitation.scorelist");

		}
		$this->tpl->display("sanitation/score.update.tpl.php");
	}
	/** 卫生分数值删除 **/
	public function actionScoreDel()
	{
		$score_id = $this->get->score_id;
		$this->db->query("DELETE  FROM `tbl_sanitation_score` WHERE score_id='{$score_id}'");
		// 跳转
		Helper_Client::location("删除成功", "index.php?a=sanitation.scorelist");
	}
	/** ---------------------------------- 登记表 ------------------------------------- **/
	/** 卫生检查登记列表 **/
	public function actionList ()
	{
		// 获取当前学期id
		//$semester_data = $this->model->semester->getNew();

		// 接收周次
		$sanitation_week_num   = $this->get->sanitation_week_num;
		// 学期id（如果有值则覆盖以前id获取最新学期id）
		$semester_id   = $this->get->semester_id;
		//if(empty($semester_id)){$semester_id   = $semester_data['semester_id'];}
		// 获取当次学期学年名称
		$semester_data = $this->model->semester->getInfo($semester_id);
		$semester_data['semester_name'] = $this->semester_name[$semester_data['semester_name']];
		// 获取所有学期,学期的名字
		$semester_lists = $this->model->semester->getAll();

		// 搜索 上面有接收的

		$where = 'WHERE 1=1 ';
		//  进入那个学期的入口
		if(!empty($semester_id)){
			$where .= " AND semester_id = '{$semester_id}' ";
			$this->tpl->assign("semester_id", $semester_id);
		}
		if(!empty($sanitation_week_num)){
			$where .= " AND sanitation_week_num = '{$sanitation_week_num}' ";
			$this->tpl->assign("sanitation_week_num", $sanitation_week_num);
		}

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_sanitation` {$where} ");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		// 星期数值
		$week_num = $this->week_num;
		if (!empty($semester_id)) {
			$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_class` ORDER BY class_id ASC");
			$week_number = $sanitation_week_num;
			$this->tpl->assign('weeknum',$week_number);
			foreach ($class_lists as $key => $value){
				$normal = 0;$other = 0;
				foreach ($this->week_num as $k => $v){
					$info= $this->db->fetchRow("select * from tbl_sanitation WHERE `semester_id` = {$semester_id} AND `sanitation_week_num` = '{$week_number}' and `class_id` = {$value['class_id']} and `sanitation_week` = {$v} ORDER BY sanitation_week ASC ");
					//if(!$info){}未选择的周  目前无用
					$class_lists[$key]['infos'][] = $info;
					$normal += $info['sanitation_rule'];
					$other += $info['sanitation_rests'];
				}
				$class_lists[$key]['infos'][] = array('sanitation_rule'=>$normal,'sanitation_rests'=>$other);
			}
		}

		//        foreach ($class_lists as $key => $value){
		//            foreach ($this->week_num as $k => $v){
		//                $info= $this->db->fetchRow("select a.*,b.grade_id,b.grade_name from tbl_sanitation as a ,tbl_grade as b,tbl_class as c WHERE a.class_id = c.class_id and c.grade_id = b.grade_id and a.`semester_id` = {$semester_id} AND a.`sanitation_week_num` = '{$sanitation_week_num}' and a.`class_id` = {$value['class_id']} and a.`sanitation_week` = {$v} ORDER BY sanitation_week ASC ");
		//                //exit;//if(!$info){}未选择的周  目前无用
		//                $grade_name = $info['grade_name'];
		//                if(empty($info['grade_id'])){
		//                    unset($class_lists[$key]);
		//                    continue;
		//                }
		//                if($info['grade_id'] == $class_lists[$key-1]['grade_id']){
		//                    //echo $grade_id;
		//                    unset($class_lists[$key-1]);
		//                }
		//
		//                $grade_id = $info['grade_id'];//当前年级
		//                $class_lists[$key]['grade_name'] = $grade_name;
		//                $class_lists[$key]['infos'][$grade_id]['sanitation_rule'] += $info['sanitation_rule'];
		//                //var_dump($info);exit;
		//                $class_lists[$key]['infos'][$grade_id]['sanitation_rests'] += $info['sanitation_rests'];
		//            }
		//        }
		//var_dump($class_lists);
		//		$sanitation_lists = $this->db->fetchAll("SELECT * FROM `tbl_sanitation` {$where} ORDER BY sanitation_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		// 生成模板
		//		foreach($sanitation_lists as $key => $value){
		//			// 班级名称
		//			$class_data = $this->model->class->getInfo($value['class_id']);
		//			foreach($week_num as $k => $v){
		//				$list[$class_data['class_name']]['list'][$v]['sanitation_rule'] ='';
		//				$list[$class_data['class_name']]['list'][$v]['sanitation_rests'] ='';
		//			}
		//		}
		//
		//		// 生成数据
		//		foreach($sanitation_lists as $key=>$value){
		//			// 班级名称
		//			$class_data = $this->model->class->getInfo($value['class_id']);
		//			foreach($week_num as $k => $v){
		//				// 常规
		//				$list[$class_data['class_name']]['list'][$value['sanitation_week']]['sanitation_rule'] =$value['sanitation_rule'];
		//				// 其他
		//				$list[$class_data['class_name']]['list'][$value['sanitation_week']]['sanitation_rests'] =$value['sanitation_rests'];
		//
		//			}
		//			// 合计
		//			$list[$class_data['class_name']]['content']['sanitation_rests'] += $value['sanitation_rests'];
		//			$list[$class_data['class_name']]['content']['sanitation_rule'] += $value['sanitation_rule'];
		//
		//		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("list", $list);
		$this->tpl->assign('class_lists',$class_lists);
		$this->tpl->assign("sanitation_week_num", $sanitation_week_num);
		$this->tpl->assign("semester_data", $semester_data);

		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->display('sanitation/m.list.tpl.php');

	}
	/** 卫生检查登记列表 **/
	public function actionAdd ()
	{
		// 所有分数值
		$score_lists = $this->db->fetchAll("SELECT * FROM `tbl_sanitation_score` ORDER BY score_id ASC");
		// 获取当前学期id,获得学年学期名称
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];
		$semester_data = $this->model->semester->getInfo($semester_id);
		$semester_data['semester_name'] = $this->semester_name[$semester_data['semester_name']];
		// 周次
		$week_lists = $this->db->fetchAll("SELECT * FROM `tbl_week` WHERE semester_id={$semester_id}");
		$week_num = $this->week_num;
		// 所有年级
		$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_class` ORDER BY class_id ASC");
		$week_number = $this->get->week_number;
		$this->tpl->assign('weeknum',$week_number);
		foreach ($class_lists as $key => $value){
			foreach ($this->week_num as $k => $v){
				$info= $this->db->fetchRow("select * from tbl_sanitation WHERE `semester_id` = {$semester_id} AND `sanitation_week_num` = '{$week_number}' and `class_id` = {$value['class_id']} and `sanitation_week` = {$v} ORDER BY sanitation_week ASC ");
				//if(!$info){}未选择的周  目前无用
				$class_lists[$key]['infos'][] = $info;
			}
		}
		//var_dump($class_lists);exit;
		// 星期数值


		if ($_POST){

			$week     	= $this->post->week;
			//print_R($_POST);
			if (empty($week)) {
				Helper_Client::alert("请选择周次");
			}
			// 获得当前学月
			$sanitation_month = $this->model->semester->getWeekByMonth($semester_id,$week);

			foreach($class_lists as $key=>$value){
				foreach($week_num as $k=>$v){
					// 取每项对应的参数
					$sanitation_rule	= $_POST["sanitation_rule_{$value['class_id']}_{$v}"];
					$sanitation_rests	= $_POST["sanitation_rests_{$value['class_id']}_{$v}"];
					$sanitation_rule	=$sanitation_rule['0'];
					$sanitation_rests	=$sanitation_rests['0'];
					//var_dump($sanitation_rule);exit;
					if(!empty($sanitation_rule) || !empty($sanitation_rests)){
						// 判断这个学期的周次是否存在，不存在添加，存在修改
						$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_sanitation` WHERE semester_id = {$semester_id}  AND sanitation_week_num = '{$week_number}' AND sanitation_week = {$v} AND class_id = {$value['class_id']}");

						// 写入数据库
						$info = array(

						"class_id"      		=> $value['class_id'],
						"semester_id"      		=> $semester_id,
						// 周次
						"sanitation_week_num"   => $week,
						// 星期几
						"sanitation_week"		=> $v,
						"sanitation_month"		=> $sanitation_month['semester_month'],
						"sanitation_rule"		=> $sanitation_rule,
						"sanitation_rests"		=> $sanitation_rests,
						"status"                => 1,
						"create_time"           => date("Y-m-d H:i:s"),
						"update_time"           => "0000-00-00 00:00:00",
						);
						// 空=修改，有值=添加
						if(!empty($class_lists)){
							$this->db->do_update("tbl_sanitation", $info, "semester_id = {$semester_id}  AND sanitation_week_num = '{$week}' AND sanitation_week = {$v} AND class_id= {$value['class_id']}");
						}else{
							$this->db->do_insert("tbl_sanitation", $info);
						}
					}
				}
			}


			// 跳转
			Helper_Client::location("登记成功", "index.php?a=sanitation.list&semester_id={$semester_id}&sanitation_week_num={$week}");
		}

		$this->tpl->assign("score_lists", $score_lists);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->assign("class_lists", $class_lists);

		$this->tpl->assign("week_num", $week_num);
		$this->tpl->display('sanitation/add.tpl.php');
	}
	/** ----------------------------- 卫生检查管理学月汇总表 ----------------------------- **/
	/** 卫生检查学月汇总列表 **/
	public function actionMonthList ()
	{
		// 搜索
		$semester_id     	= $this->get->semester_id;
		$semester_month     = $this->get->semester_month;
		// 不搜索，就显示当前学期id
		if(empty($semester_id)){
			// 获取当前学期id,获得学年学期名称
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];
		}
		if(empty($semester_month)){
			// 获取当前学期id,获得学年学期名称
			$row =  $this->model->semester->getWeekByTime(date("Y-m-d",time()));
			$semester_month = $row['semester_month'];
		}
		//var_dump($row);exit;
		// 所有学期名称
		$semester_lists =  $this->model->semester->getIni();
		// 所有班级
		$class =  $this->model->class->getIni();

		// 学月
		$month_lists = $this->model->semester->getMonthAll($semester_id);
		
		$semester_data =  $this->model->semester->getInfo($semester_id);
		$semester_data['semester_name'] = $this->semester_name[$semester_data['semester_name']];

		$where = 'WHERE 1=1 ';

		//  进入那个学期的入口
		if(!empty($semester_id)){
			$where .= " AND semester_id = '{$semester_id}' ";
			$this->tpl->assign("semester_id", $semester_id);

		}
		if(!empty($semester_month)){
			$where .= " AND sanitation_month = '{$semester_month}' ";
			$this->tpl->assign("semester_month", $semester_month);
		}

		$weeks = $this->model->semester->getWeekAll($semester_id,$semester_month );
		//if(empty($weeks))$weeks = array();

		$this->tpl->assign('weeks',$weeks);

		$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_class` ORDER BY class_id ASC");
		//$week_number = $semester_month;
		//$this->tpl->assign('weeknum',$week_number);
		foreach ($class_lists as $key => $value){
			$normal = 0;$other = 0;
			if (is_array($weeks) && count($weeks)>0 ){//学期被清除
				foreach ($weeks as $k => $v){
					$info= $this->db->fetchAll("select * from tbl_sanitation WHERE `semester_id` = {$semester_id} AND `sanitation_month` = '{$semester_month}' and `class_id` = {$value['class_id']} and `sanitation_week_num` = {$v} ORDER BY sanitation_week_num ASC ");
					//if(!$info){}未选择的周  目前无用
					$rule = 0 ; $resets = 0;
					foreach($info as $ke => $ve){
						$rule += $ve['sanitation_rule'];
						$resets += $ve['sanitation_rests'];
					}
					$class_lists[$key]['infos'][] = array('sanitation_rule'=>$rule,'sanitation_rests'=>$resets);
					$normal += $rule;
					$other += $resets;
				}
			}
			$class_lists[$key]['infos'][] = array('sanitation_rule'=>$normal,'sanitation_rests'=>$other);
		}
		//var_dump($class_lists);
		$this->tpl->assign('class_lists',$class_lists);

		// 数据集
		//		$sanitation_lists = $this->db->fetchAll("SELECT class_id,sanitation_week_num,sanitation_week,SUM(sanitation_rests) as sanitation_rests,SUM(sanitation_rule) as sanitation_rule FROM `tbl_sanitation` {$where} GROUP BY class_id,sanitation_week_num ORDER BY class_id ASC");
		//
		//
		//		// 生成模板
		//		foreach($sanitation_lists as $key => $value){
		//			for($i=1;$i<=4;$i++){
		//
		//				$list[$class[$value['class_id']]]['list'][$i]['sanitation_rule'] ='';
		//				$list[$class[$value['class_id']]]['list'][$i]['sanitation_rests'] ='';
		//			}
		//		}
		//
		//		// 添加数据
		//		foreach($sanitation_lists as $key => $value){
		//
		//			$list[$class[$value['class_id']]]['list'][$value['sanitation_week']]['sanitation_rests'] = $value['sanitation_rests'];
		//			$list[$class[$value['class_id']]]['list'][$value['sanitation_week']]['sanitation_rule']  = $value['sanitation_rule'];
		//
		//			// 每个班级每月合计
		//			$list[$class[$value['class_id']]]['all']['sanitation_rests']+=$value['sanitation_rests'];
		//			$list[$class[$value['class_id']]]['all']['sanitation_rule']+=$value['sanitation_rule'];
		//
		//		}
        $len = count($weeks);
        $this->tpl->assign("len", $len);
		$this->tpl->assign("list", $list);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("month_lists", $month_lists);
		$this->tpl->assign("semester_month", $semester_month);
		$this->tpl->display('sanitation/m.month.list.tpl.php');

	}
	/** -------------------------- 卫生检查管理学期汇总表 ------------------------------ **/
	public function actionSemesterList ()
	{

		// 搜索
		$semester_id     	= $this->get->semester_id;

		// 不搜索，就显示当前学期id
		if(empty($semester_id)){
			// 获取当前学期id,获得学年学期名称
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];
		}

		// 所有学期名称
		$semester_lists =  $this->model->semester->getIni();
		// 所有班级
		$class =  $this->model->class->getIni();

		// 学月
		$semester_month = $this->db->fetchAll("SELECT distinct `semester_month` FROM `tbl_week` WHERE semester_id='{$semester_id}'");
		//var_dump($semester_month);
		$semester_data =  $this->model->semester->getInfo($semester_id);
		//$semester_data['semester_name'] = $this->semester_name[$semester_data['semester_name']];

		$where = 'WHERE 1=1 ';

		//  进入那个学期的入口
		if(!empty($semester_id)){
			$where .= " AND semester_id = '{$semester_id}' ";
			$this->tpl->assign("semester_id", $semester_id);

		}

		$week_num = $this->week_num;
		$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_class` ORDER BY class_id ASC");
		// $this->tpl->assign('weeknum',$week_number);
		foreach ($class_lists as $key => $value){
			$normal = 0;$other = 0;
			foreach ($semester_month as $k => $v){
				$info= $this->db->fetchRow("select sum(sanitation_rule) as sanitation_rule,sum(sanitation_rests) as sanitation_rests from tbl_sanitation WHERE `semester_id` = {$semester_id} AND `class_id` = {$value['class_id']} and `sanitation_month` = {$v['semester_month']} ORDER BY sanitation_month ASC ");
				//if(!$info){}未选择的周  目前无用
				$class_lists[$key]['infos'][] = $info;
				$normal += $info['sanitation_rule'];
				$other += $info['sanitation_rests'];
			}
			$class_lists[$key]['infos'][] = array('sanitation_rule'=>$normal,'sanitation_rests'=>$other);
		}
		$this->tpl->assign('class_lists',$class_lists);

		//		// 数据集
		//		$sanitation_lists = $this->db->fetchAll("SELECT class_id,sanitation_month,SUM(sanitation_rests) as sanitation_rests,SUM(sanitation_rule) as sanitation_rule FROM `tbl_sanitation` {$where} GROUP BY class_id,sanitation_month ORDER BY class_id ASC");
		//
		//		// 生成模板
		//		foreach($sanitation_lists as $key => $value){
		//			foreach($semester_month as $k => $v){
		//
		//				$list[$class[$value['class_id']]]['list'][$v['semester_month']]['sanitation_rule'] ='';
		//				$list[$class[$value['class_id']]]['list'][$v['semester_month']]['sanitation_rests'] ='';
		//
		//			}
		//
		//		}
		//
		//		// 添加数据
		//		foreach($sanitation_lists as $key => $value){
		//
		//			$list[$class[$value['class_id']]]['list'][$value['sanitation_month']]['sanitation_rests'] = $value['sanitation_rests'];
		//			$list[$class[$value['class_id']]]['list'][$value['sanitation_month']]['sanitation_rule']  = $value['sanitation_rule'];
		//
		//			// 每个班级每月合计
		//			$list[$class[$value['class_id']]]['all']['sanitation_rests']+=$value['sanitation_rests'];
		//			$list[$class[$value['class_id']]]['all']['sanitation_rule']+=$value['sanitation_rule'];
		//
		//		}


		$this->tpl->assign("list", $list);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("semester_lists", $semester_lists);
        $len = count($semester_month);
        $this->tpl->assign("len", $len);
		$this->tpl->assign("semester_month", $semester_month);

		$this->tpl->display('sanitation/m.semester.list.tpl.php');

	}

}

?>